package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.GeneratePageService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;


/**
 * 保存三张表信息
 */
@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    ApAuthorMapper apAuthorMapper;

    @Autowired
    ApArticleContentMapper apArticleContentMapper;

    @Autowired
    ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    GeneratePageService generatePageService;

    /**
     * 保存三张表信息
     * @param articleDto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto articleDto) {
        log.info("方法被调用执行,saveArticle 保存文章三表信息, 参数:{}",articleDto);

        // 解析dto 封装 ap_article
        ApArticle apArticle = new ApArticle();

            // try cath 处理
        try {
            BeanUtils.copyProperties(articleDto, apArticle);
        } catch (BeansException e) {
            e.printStackTrace();
        }


        // 补全 article
        ApAuthor apAuthor = apAuthorMapper.selectOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, articleDto.getWmUserId()));
        apArticle.setAuthorId((long)apAuthor.getId());
        apArticle.setAuthorName(apAuthor.getName());

        // 根据是否包含articleId判断是修改还是新增
        if (articleDto.getId() == null) {
            // 新增
            apArticle.setCollection(0); // 收藏
            apArticle.setLikes(0);  // 点赞
            apArticle.setComment(0); // 评论
            apArticle.setViews(0); // 阅读
            save(apArticle);

        } else {
            // 修改
            updateById(apArticle);
            // 如果之前包含config和 content 信息 删除掉
            apArticleConfigMapper.delete(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, apArticle.getId()));
            apArticleContentMapper.delete(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));

            updateById(apArticle);
        }
        //添加  config信息
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setIsForward(true);
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsDelete(false);
        apArticleConfig.setArticleId(apArticle.getId());

        apArticleConfigMapper.insert(apArticleConfig);

        // 添加 content信息
        //保存文章article_content 数据
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setContent(articleDto.getContent());
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContentMapper.insert(apArticleContent);

        // Todo: 异步生成文章静态页面

        log.info("方法被调用执行,saveArticle 保存文章三表信息成功");
        generatePageService.generateArticlePage(apArticle.getId());

        System.out.println("=========静态页面调用后的操作========");
        //返回文章ID
        return ResponseResult.okResult(apArticle.getId());
    }
}